const {
  CurrencyShopModel
} = require('../../utils/currencyshop-model.js')
const currencyShopModel = new CurrencyShopModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shop: {},
    appletMobile: '',
    currentBannerIndex: 0,
    currentCommonIndex: 0,
    defaultCurrent: 0,
    modules: []
  },
  getModule() {
    const { merchantId, shopId, brandId } = wx.getStorageSync('shopInfo')
    currencyShopModel.queryHomeModule({
      merchantId,
      shopId,
      brandId,
      pageType: 1
    }).then(data => {
      data = data || {}
      const modules = data.modules || []
      modules.forEach(moduleItem => {
        if (moduleItem.moduleType == 8) {
          moduleItem.contents.forEach(item => {
            const options = item.options
            if (options.goodsPrices) {
              options.goodsPrices.forEach(goods => {
                const price = goods
                const formatPrice = Number(goods.amount).toFixed(2)
                const split = formatPrice.split('.')
                price.int = split[0]
                price.float = split[1]
                price.formatPrice = formatPrice
                price.formatDiscoutPrice = split[0].length > 1 ? `${split[0].substr(0, split[0].length - 2)}?${split[0].substr(split[0].length - 1, split[0].length)}` : '?'
              })
            }
          })
        }
      })
      this.setData({
        currentBannerIndex: 0,
        currentCommonIndex: 0,
        defaultCurrent: 0,
        modules
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none'
      })
    })
  },
  currentBannerChange(e) {
    this.setData({
      currentBannerIndex: e.detail.current
    })
  },
  currentCommonChange(e) {
    this.setData({
      currentCommonIndex: e.detail.current
    })
  },
  clickBannerItem(e) {
    wx.reportAnalytics('home_banner')
    const options = e.currentTarget.dataset.options || {}
    if (options.jumpType == 1) {
      if (options.jumpUrl) {
        let goUrl = '';
        if (options.jumpUrl.indexOf('?') > -1) {
          goUrl = `${options.jumpUrl}&shopId=${this.data.shop.shopId}&shopName=${this.data.shop.shopName}&cityCode=${this.data.shop.cityCode}&cityName=${this.data.shop.cityName}`
        } else {
          goUrl = `${options.jumpUrl}?shopId=${this.data.shop.shopId}&shopName=${this.data.shop.shopName}&cityCode=${this.data.shop.cityCode}&cityName=${this.data.shop.cityName}`
        }
        wx.navigateTo({
          url: goUrl
        })
      }
    } else if (options.jumpType == 2) {
      wx.navigateToMiniProgram({
        appId: options.jumpAppId,
        path: options.jumpUrl || 'pages/index/index',
        extraData: {},
        envVersion: 'release',
        success(res) {}
      })
    } else if (options.jumpType == 3) {
      if (options.jumpUrl) {
        wx.navigateTo({
          url: `/pages/h5/index?url=${encodeURIComponent(`${options.jumpUrl}?phoneNumber=${this.data.appletMobile}`)}`,
        })
      }
    }
  },
  goGameList(e) {
    const options = e.currentTarget.dataset.options || {}
    if (options.content_more_link) {
      wx.navigateTo({
        url: options.content_more_link
      })
    }
  },
  goContent(e) {
    const options = e.currentTarget.dataset.options || {}
    if (options.redirect_type == '1') {
      wx.navigateToMiniProgram({
        appId: options.app_id,
        path: options.link || 'pages/index/index',
        extraData: {},
        envVersion: 'release',
        success(res) {}
      })
    } else if (options.redirect_type == '0' && options.link) {
      wx.navigateTo({
        url: options.link
      })
    }
  },
  goGoodsDetail(e) {
    const {
      goods
    } = e.currentTarget.dataset
    // type暂时只有游戏币固定为0 从首页进入商品详情页entrance为0
    wx.navigateTo({
      url: `/pages/currencyShop/currencyDetail/index?shopName=${this.data.shop.shopName}&currency=${encodeURIComponent(JSON.stringify(goods))}&shopId=${this.data.shop.shopId}&type=${0}&entrance=${0}`
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const shopInfo = wx.getStorageSync('shopInfo')
    this.setData({
      shop: shopInfo
    })
    this.getModule()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  }
})